<?php
//待删除，重新过一遍竞拍前后台
namespace app\home\controller;

use app\common\model\ShopOrderModel;
use app\common\model\ShopPromotionAuctionLogModel;
use app\common\model\ShopPromotionAuctionModel;
use think\Request;

class AuctionLogController extends BaseController
{
    private $auctionLogModel;
    private $auctionModel;
    private $shopOrderModel;

    public function __construct(Request $request)
    {
        $this->auctionLogModel = new ShopPromotionAuctionLogModel();
        $this->auctionModel = new ShopPromotionAuctionModel();
        $this->shopOrderModel = new ShopOrderModel();
        parent::__construct($request);
    }

    /**
     * 保证金缴付
     */
    public function Bond(){
        $data['goods_id'] = $this->request->param('goods_id');
        $data['sku_id'] = $this->request->param('sku_id');
        $data['pay_sn'] = rand_number();
        $auctionInfo = $this->auctionModel->where('goods_id',$data['goods_id'])->find();
        $data['goods_number'] = $auctionInfo['number'];
        $data['member_uid']   = $this->getMemberUid();
        $data['action_time']  = time();
        $data['money']        = $auctionInfo['margin_money'];
        $this->auctionLogModel->addData($data);
    }
    /*
     * 添加竞拍
     */
    public function add()
    {
        $data['money'] = $this->request->param('money');
        $data['goods_id'] = $this->request->param('goods_id');
        $data['sku_id'] = $this->request->param('sku_id');
        $data['member_uid'] = $this->getMemberUid();
        $data['action_time'] = time();
        $data['pay_type'] = 1;
        $data['winning'] = 1;
        if(($data['action_time']<=$this->auctionModel->where('goods_id',$data['goods_id'])->value('end_time'))&& ($data['money']>$this->auctionModel->where('goods_id',$data['goods_id'])->value('current_price'))){
            $this->auctionLogModel->where(['goods_id'=>$data['goods_id'],'winning'=>1])->setField('winning',0);
            $this->auctionLogModel->addData($data);
            $offerTimes = $this->auctionLogModel->where(['goods_id'=>$data['goods_id'],'pay_type'=>1])->count();
            $offerPerson = $this->auctionLogModel->where('goods_id',$data['goods_id'])->group('member_uid')->count();
            $this->auctionModel->where('goods_id',$data['goods_id'])->setField(['current_price'=>$data['money'],'offer_times'=>$offerTimes,'offer_person'=>$offerPerson]);
        }
        //return $this->fetch();
    }

    public function addorder(){
        $data['goods_id'] = $this->request->param('goods_id');
        $data['addressId'] = $this->request->param('addressId');
        $data['orderMessage'] = $this->request->param('orderMessage');
        $data['express_id'] = $this->request->param('express_id');
        $data['invId'] = $this->request->param('invId');
        $data['pay_code'] = $this->request->param('pay_code');
        $this->shopOrderModel->auctionOrder($data['goods_id'], $data['addressId'], $data['orderMessage'], $data['express_id'], $data['invId'], $payCode = $data['pay_code']);
    }
}
